package com.kanshu.ksgb.fastread.doudou.module.book.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig;
import com.kanshu.ksgb.fastread.doudou.base.baseui.BaseListFragment;
import com.kanshu.ksgb.fastread.doudou.common.net.bean.PageRequestParams;
import com.kanshu.ksgb.fastread.doudou.common.util.ARouterUtils;
import com.kanshu.ksgb.fastread.doudou.common.business.commonbean.BookInfo;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.adapter.SelectedBottomAdapter;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.presenter.BookCityPresenter;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.retrofit.SelectedCategoryReq;

/**
 * Created by wcy on 18/5/22.
 */

public class CategoryBooksFragment extends BaseListFragment<BookInfo> implements IItemChangedListener {
    private String mCategoryId1;
    private String mCategoryId2;
    private String mSite;
    private BookCityPresenter mPresenter;
    public static CategoryBooksFragment newInstance(String categoryId1, String categoryId2, String site) {
        CategoryBooksFragment fragment = new CategoryBooksFragment();
        Bundle bundle = new Bundle();
        bundle.putString(SubCategoryFragment.EXTRA_CATEGORY_ID_1, categoryId1);
        bundle.putString(SubCategoryFragment.EXTRA_CATEGORY_ID_2, categoryId2);
        bundle.putString(SubCategoryFragment.EXTRA_SITE, site);
        fragment.setArguments(bundle);
        return fragment;
    }

    private void parseArugment() {
        Bundle arguments = getArguments();
        if (arguments == null) {
            return;
        }
        mCategoryId1 = arguments.getString(SubCategoryFragment.EXTRA_CATEGORY_ID_1);
        mCategoryId2 = arguments.getString(SubCategoryFragment.EXTRA_CATEGORY_ID_2);
        mSite = arguments.getString(SubCategoryFragment.EXTRA_SITE);
        mRequestParams = new SelectedCategoryReq();
        SelectedCategoryReq req = (SelectedCategoryReq) mRequestParams;
        req.category_id_1 = mCategoryId1;
        req.category_id_2 = mCategoryId2;
        req.site = mSite;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        parseArugment();
        mPresenter = new BookCityPresenter(lifeCyclerSubject);
        mPresenter.setBookView(this);
    }

    @Override
    protected BaseQuickAdapter provideAdapter() {
        mAdapter = new SelectedBottomAdapter(getActivity(), mList);
        mAdapter.setOnItemClickListener((view, position) -> {
            ARouterUtils.toActivity(ARouterConfig.BOOK_DETAIL, "book_id", mList.get(position - mAdapter.getHeaderViewsCount()).book_id);
        });
        return mAdapter;
    }

    @Override
    protected void updateViews() {
        super.updateViews();
        getContentAysnc(mRequestParams);
    }

    @Override
    protected void getContentAysnc(PageRequestParams params) {
        mPresenter.getCategoryBooks((SelectedCategoryReq) params);
    }

    @Override
    public void onClassifyIdChanged(String classify_id) {
        ((SelectedCategoryReq)mRequestParams).category_id_2 = classify_id;
        getContentAysnc(mRequestParams);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mPresenter.detachView();
    }
}
